package yl.hs.bmipfull.service.finance.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.finance.IReceiptSettleMainDao;
import yl.hs.bmipfull.daos.finance.IReceivePaymentRecordDao;
import yl.hs.bmipfull.pojo.material.PurchaseContractEntity;
import yl.hs.bmipfull.service.finance.IReceiptLedgerService;
import yl.hs.bmipfull.viewmodels.PageResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReceiptLedgerServiceImpl extends FinanceService implements IReceiptLedgerService {

    @Override
    public PageResult<Map<String,Object>> convertToLedger(PageResult<PurchaseContractEntity> contractResult){
        var contractSNList = contractResult.getData().stream().map(t->t.getSn()).toList();
        PageResult<Map<String,Object>> result = new PageResult<>(contractResult.pageDataSize);
        result.setRecords(contractResult.getRecords());
        result.setPage(contractResult.getPage());
        List<Map<String,Object>> list = new ArrayList<>();

        if(mainDao.queryContractSettleLastVerification()<1){
            return result;
        }
        var settleList = mainDao.queryContractLastSettle(contractSNList);
        contractResult.getData().forEach(t->{
            Map<String,Object> item = new HashMap<>();
            item.put("contractSn",t.getSn());//合同编号
            item.put("supplierName",t.getSupplierName());//供应商名称
            item.put("supplierSN",t.getSupplierSN());
            var settleOpt = settleList.stream().filter(s->t.getSn().equals(s.getContractSN())).findFirst();
            if(!settleOpt.isEmpty()) {
                var settle = settleOpt.get();
                item.put("settleTime", settle.getSettleTime());//结算时间
                item.put("currentQuantity", settle.getCurrentQuantity());//本期结算重量(吨)
                item.put("totalQuantity", settle.getTotalQuantity());//累计计算重量(吨)
                item.put("currentAmount", settle.getCurrentAmount());//本期结算金额
                item.put("totalAmount", settle.getTotalAmount());//累计结算金额
                item.put("currentPayedAmount", settle.getCurrentPayedAmount());//本期付款金额
                item.put("totalPayedAmount",recordDao.getNetReceiptsFu(t.getSn()));//累计付款金额
                item.put("currentOrderCount", settle.getCurrentOrderCount());//本期结算车次
                item.put("totalOrderCount", settle.getTotalOrderCount());//累计结算车次
            }
            else
            {
                item.put("settleTime", "");
                item.put("currentQuantity", 0.00);
                item.put("totalQuantity", 0.00);
                item.put("currentAmount", 0.00);
                item.put("totalAmount", 0.00);
                item.put("currentPayedAmount", 0.0);
                item.put("totalPayedAmount",recordDao.getNetReceiptsFu(t.getSn()));
                item.put("currentOrderCount", 0D);
                item.put("totalOrderCount", 0);
            }
            list.add(item);
        });

        result.setData(list);
        return result;
    }

    @Autowired
    IReceiptSettleMainDao mainDao;
    @Autowired
    IReceivePaymentRecordDao recordDao;
}
